//
//  MyOfUserDetail.m
//  PetAPP
//
//  Created by apple on 16/3/6.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "MyOfUserDetail.h"
#import "ModelOfUserinfo.h"
#import "CellOfUserDetail.h"
#import <YYWebImage.h>
#import "CellOfLeave.h"
#import "SexForDetail.h"
#import "AddressForDetail.h"
#import "TwitterForDetail.h"
#import "UserNameFroDetail.h"

@interface MyOfUserDetail () <UITableViewDataSource, UITableViewDelegate, CellOfLeaveDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataArr;

@end

@implementation MyOfUserDetail

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"基础资料设置";
    self.view.backgroundColor = [UIColor whiteColor];
    [self handleData];
    [self createSubViews];
}

- (void)handleData {
    self.dataArr = @[@[@"乐宠ID", @"昵称", @"手机号", @"性别", @"所在地", @"签名"],@[self.modelOfUser.uid,
                                                                       self.modelOfUser.username,
                                                                       self.modelOfUser.mobile,
                                                                       [NSString stringWithFormat:@"%@", self.modelOfUser.gender.integerValue < 2 ? @"男" : @"女"],
                                                                       [NSString stringWithFormat:@"%@ %@ %@", self.modelOfUser.province, self.modelOfUser.city, self.modelOfUser.area],
                                                                       self.modelOfUser.twitter].mutableCopy].mutableCopy;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 6 || indexPath.row == 0 || indexPath.row == 2) {
    }else {
        switch (indexPath.row) {
            case 1:{
                if ([self.modelOfUser.changename  isEqual: @0]) {
                    UserNameFroDetail *userName = [[UserNameFroDetail alloc] init];
                    userName.edgesForExtendedLayout = UIRectEdgeNone;
                    [self.navigationController pushViewController:userName animated:YES];
                    [userName release];
                    userName.textContent = self.dataArr[1][1];
                    userName.block = ^(NSString *text) {
                        
                        [self.dataArr[1] replaceObjectAtIndex:1 withObject:text];
                        [self.tableView reloadData];
                    };
                    
                }else {
                    
                }
                break;
            }
            case 3:{
                SexForDetail *sex = [[SexForDetail alloc] init];
                sex.edgesForExtendedLayout = UIRectEdgeNone;
                [self.navigationController pushViewController:sex animated:YES];
                [sex release];
                sex.block = ^(NSString *sex) {
                    [self.dataArr[1] replaceObjectAtIndex:3 withObject:sex];
                    [self.tableView reloadData];
                
                };
                
                break;
            }
            case 4:{
                
                break;
            }
            case 5:{
                TwitterForDetail *twitter = [[TwitterForDetail alloc] init];
                twitter.edgesForExtendedLayout = UIRectEdgeNone;
                [self.navigationController pushViewController:twitter animated:YES];
                twitter.textContent = self.dataArr[1][5];
                twitter.block = ^(NSString *text) {
                    
                    [self.dataArr[1] replaceObjectAtIndex:5 withObject:text];
                    [self.tableView reloadData];
                };
                [twitter release];
                
                break;
            }
                
            default:
                break;
        }
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 6) {
        CellOfLeave *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfLeave"];
        cell.delegate = self;
        return cell;
    }else {
        
        CellOfUserDetail *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfCell"];
        
        [cell handleDataWithName:self.dataArr[0][indexPath.row] andContent:self.dataArr[1][indexPath.row]];
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 6) return 100;
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 7;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (void)createSubViews {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[CellOfLeave class] forCellReuseIdentifier:@"poolOfLeave"];
    [self.tableView registerClass:[CellOfUserDetail class] forCellReuseIdentifier:@"poolOfCell"];
    self.tableView.separatorStyle = 1;
    /** 结局tableView没有数据仍然有横线的问题 */
    self.tableView.tableFooterView = [UIView new];
    /** 解决tableView横线不是从头开始的问题 */
    [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    [self.tableView setLayoutMargins:UIEdgeInsetsZero];
#pragma mark - 设置tableView的头部
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(view.center.x - 50, 25, 100, 100)];
    [imageView yy_setImageWithURL:[NSURL URLWithString:self.modelOfUser.picbig] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
    [view addSubview:imageView];
    [imageView release];
    
    imageView.layer.cornerRadius = 50;
    imageView.layer.masksToBounds = YES;
    
    self.tableView.tableHeaderView = view;
    [view release];
    
    UIView *viewOfLine = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view.frame) - 1, CGRectGetWidth(view.frame), 1)];
    [view addSubview:viewOfLine];
    [viewOfLine release];
    viewOfLine.layer.borderColor = [UIColor colorWithRed:0.8903 green:0.8903 blue:0.8903 alpha:1.0].CGColor;
    viewOfLine.layer.borderWidth = 1;
    
}
/** 解决tableView横线不是从头开始的问题 */
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)handleLeaveLogin {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"亲，确定要退出吗!" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"loginState"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"handleData" object:nil userInfo:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:actionOK];
    [alert addAction:actionCancel];
    //  模态推出.
    [self presentViewController:alert animated:YES completion:^{}];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
